/**
 * Created by c.y on 2018/5/16
 * 文件说明: 产品查看的数据的封装
 */

/**
 * 获取Attr的列表, prodElements产品组件的元素列表
 * @param prodElements { Array } 产品showEles数组，即产品属性的组成的数组
 * @return {Array}
 */
function getAttrList(prodElements) {
    // 获取用户修改的数据
    let attrList = [];
    if (prodElements instanceof Array) {
        prodElements.map(function (listItem) {
            listItem.attrs.forEach(function (item) {
                attrList.push(item.value);
            });
        });
    }
    return attrList;
}


/**
 * 产品基本信息的获取
 * @param prodBaseInfo 产品的基本信息
 * @param prodTypes 产品的分类list
 * @return { Object } 产品的基本信息
 */
function getBaseInfo(prodBaseInfo, prodTypes) {
    let prodType = ''; // 产品的分类
    for (let i = 0; i < prodTypes.length; i++) {
        if (prodTypes[i].code === prodBaseInfo.prodType + '') {
            prodType = prodTypes[i].name;
        }
    }
    return {
        prodId: prodBaseInfo.prodId, // 产品编号
        prodName: prodBaseInfo.prodName, // 产品名称
        prodType: prodType, // 产品分类
        orgName: prodBaseInfo.orgName, // 产品提供方
    }
}


/**
 * 产品的列表信息的获取
 * @param initProdListInfo
 * @return { Object } 产品的列表信息
 */
function getListInfo(initProdListInfo) {
    let prodListInfo = getAttrList(initProdListInfo.showEles);
    return {
        logoImgUrl: prodListInfo[0], // logo
        prodFeature: prodListInfo[1], // 产品特点
        prodName: prodListInfo[2], // 产品名称
        prodLimit: prodListInfo[3], // 额度范围
        limitUnit: prodListInfo[4], // 额度范围的单位
        prodDesc: prodListInfo[5], // 产品描述
        prodRate: prodListInfo[6], // 产品利率描述
        timeLimit: prodListInfo[7] // 期限描述
    }
}


/**
 * 产品的详情信息的获取
 * @param prodDetailInfo
 * @return { Object } 产品的详情信息
 */
function getDetailInfo(prodDetailInfo) {
    let prodDetailHead = getAttrList(prodDetailInfo.components[0].showEles);   // 产品头部信息
    let prodDetailRate =  getAttrList(prodDetailInfo.components[1].showEles); // 利率--内容
    let prodDetailApply =  getAttrList(prodDetailInfo.components[2].showEles); // 申请条件--内容
    let prodDetailProcess =  getAttrList(prodDetailInfo.components[3].showEles); // 进件流程---内容
    let prodDetailMaterial =  getAttrList(prodDetailInfo.components[4].showEles); // 所需材料---内容
    let prodDetailOther = []; // 额外添加的数据
    // 判断是否有额外增加的内容(最多增加两项)
    if (prodDetailInfo.components[5] && prodDetailInfo.components[5].showEles.length) {
        prodDetailOther.push({
            title: prodDetailInfo.components[5].name,
            content: getAttrList(prodDetailInfo.components[5].showEles)
        });
    }
    if (prodDetailInfo.components[6] && prodDetailInfo.components[6].showEles.length) {
        prodDetailOther.push({
            title: prodDetailInfo.components[6].name,
            content: getAttrList(prodDetailInfo.components[6].showEles)
        });
    }
    return {
        prodName: prodDetailHead[0],
        loanAmount: prodDetailHead[1],
        loanAmountDesc: prodDetailHead[2],
        loanPeriod: prodDetailHead[3],
        loanPeriodDesc: prodDetailHead[4],
        approvalTime: prodDetailHead[5],
        approvalTimeDesc: prodDetailHead[6],
        rateStep: {  // 利率
            title: prodDetailInfo.components[1].name,
            content: prodDetailRate
        },
        applyStep: { // 申请条件
            title: prodDetailInfo.components[2].name,
            content: prodDetailApply
        },
        materialStep:{   // 所需材料
            title: prodDetailInfo.components[4].name,
            content: prodDetailMaterial
        },
        otherStep: prodDetailOther, // 额外增加的内容
        processStep:[     // 步骤信息
            {
                processImage: prodDetailProcess[0],
                processName: prodDetailProcess[1]
            },
            {
                processImage: prodDetailProcess[2],
                processName: prodDetailProcess[3]
            },
            {
                processImage: prodDetailProcess[4],
                processName: prodDetailProcess[5]
            },
            {
                processImage: prodDetailProcess[6],
                processName: prodDetailProcess[7]
            }
        ]
    }
}

/**
 * 产品第四步的步骤信息获取
 * @param prodStepInfo
 * @return { Object } 产品的步骤信息
 */
function getStepInfo(prodStepInfo) {
    // 如果不是空数组，即有产品步骤信息
    let stepNameList = []; // 步骤名称列表
    let prodStepAttrs = []; // 产品步骤字段列表
    if (prodStepInfo && prodStepInfo.length) {
        prodStepInfo.forEach(function (item) {
            stepNameList.push(item.stepName);
            prodStepAttrs.push(item.prodAttrs);
        });
        return {
            stepNameList: stepNameList,
            prodStepAttrs: prodStepAttrs
        }
    } else {
        return {
            stepNameList: [],
            prodStepAttrs: []
        }
    }
}

export default {
    getBaseInfo,
    getListInfo,
    getDetailInfo,
    getStepInfo
};
